package queue;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-12-13 19:57
 **/
public class Test_ConcurrentQueue {

    public static void main(String[] args) {

        /**
         * ConcurrentLinkedQueue 是一个基于链接节点的无界线程安全队列，它采用先进先出的规则对节点进行排序，
         * 当我们添加一个元素的时候，它会添加到队列的尾部，当我们获取一个元素时，它会返回队列头部的元素-
         */

        Queue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();

        /**
         * 添加数据， 和 add 不同的是，add 无法加入则会抛出异常，
         * offer 无法加入则返回 false
         *
         *  Inserts the specified element into this queue if it is possible to do
         *  so immediately without violating capacity restrictions.
         *  When using a capacity-restricted queue, this method is generally
         *  preferable to {@link #add}, which can fail to insert an element only
         *  by throwing an exception.
         */
        boolean a = concurrentLinkedQueue.offer("a"); //add

        /**
         * 返回队列中元素的数量，
         * 因为队列没有长度和容量的限制
         * 如果数量超过Integer.MAX_VALUE ，返回 Integer.MAX_VALUE
         *
         * Returns the number of elements in this collection.  If this collection
         * contains more than {@code Integer.MAX_VALUE} elements, returns
         * {@code Integer.MAX_VALUE}.
         */
        System.out.println(concurrentLinkedQueue.size());

        /**
         * 返回队列中头部节点，并且做删除操作
         *  Retrieves and removes the head of this queue,
         *  or returns {@code null} if this queue is empty.
         */
        System.out.println(concurrentLinkedQueue.poll());

        /**
         * 返回队列中头部节点，但是不做删除操作
         *  Retrieves, but does not remove, the head of this queue,
         *  or returns {@code null} if this queue is empty.
         */

        System.out.println(concurrentLinkedQueue.peek());

        System.out.println(concurrentLinkedQueue);

    }

}
